<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户权限管理 - SimpleHire</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        /* 统一的CSS变量 */
        :root {
            --primary-color: #5A67D8;
            --primary-hover: #434B9A;
            --text-color: #2D3748;
            --light-gray: #F7FAFC;
            --border-color: #E2E8F0;
            --danger-color: #E53E3E;
            --danger-hover: #C53030;
        }

        body {
            font-family: 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
            background-color: var(--light-gray);
            min-height: 100vh;
            color: var(--text-color);
        }

        .admin-header {
            background-color: var(--text-color);
            color: white;
            padding: 1.5rem 0;
            margin-bottom: 2rem;
        }

        .admin-header h1 {
            font-size: 1.5rem;
            margin: 0;
            font-weight: 600;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 1rem;
        }

        .header-buttons a {
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.5);
            border-radius: 8px;
            transition: background-color 0.3s ease, border-color 0.3s ease;
            padding: 0.5rem 1rem;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .header-buttons a:hover {
            background-color: rgba(255, 255, 255, 0.1);
            border-color: white;
        }

        .content-card {
            background-color: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.08);
            margin-bottom: 2rem;
        }

        .content-card h5 {
            font-weight: 600;
        }

        .table-container {
            overflow-x: auto;
        }

        .table {
            margin: 0;
        }

        .table thead th {
            border-bottom: 2px solid var(--border-color);
            padding-bottom: 1rem;
            font-weight: 600;
        }

        .table tbody tr {
            transition: background-color 0.3s ease;
        }
        .table tbody tr:nth-of-type(odd) {
            background-color: var(--light-gray);
        }
        .table tbody tr:hover {
            background-color: rgba(90, 103, 216, 0.05);
        }

        .permission-badge {
            font-size: 0.8rem;
            font-weight: 600;
            padding: 0.35em 0.65em;
        }

        .btn-action {
            padding: 0.25rem 0.5rem;
            border-radius: 6px;
            font-size: 0.875rem;
            display: inline-flex;
            align-items: center;
            gap: 4px;
        }

        .btn-delete {
            background-color: var(--danger-color);
            color: white;
            border: none;
            transition: background-color 0.3s ease;
        }
        .btn-delete:hover {
            background-color: var(--danger-hover);
        }

        .form-label {
            font-weight: 500;
            margin-bottom: 0.5rem;
        }

        .form-control {
            border-radius: 8px;
            padding: 0.75rem 1rem;
            border: 1px solid var(--border-color);
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
        }
        .form-control:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(90, 103, 216, 0.2);
        }

        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
            font-weight: 600;
            transition: background-color 0.3s ease, border-color 0.3s ease;
        }
        .btn-primary:hover {
            background-color: var(--primary-hover);
            border-color: var(--primary-hover);
        }
    </style>
</head>
<body>
<div class="admin-header">
    <div class="container d-flex justify-content-between align-items-center">
        <h1><i class="bi bi-key"></i> 用户权限管理 - <span th:text="${user.username}"></span></h1>
        <div class="header-buttons">
            <a th:href="@{/admin/users}"><i class="bi bi-arrow-left"></i> 返回用户列表</a>
            <form th:action="@{/logout}" method="post" class="d-inline">
                <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                <button type="submit" class="header-buttons" style="background: none; border: none; color: white;">
                    <i class="bi bi-box-arrow-right"></i> 登出
                </button>
            </form>
        </div>
    </div>
</div>

<div class="container">
    <div class="content-card">
        <h5 class="mb-4"><i class="bi bi-lock-fill"></i> 当前权限</h5>
        <div th:if="${permissions.isEmpty()}" class="alert alert-info">该用户目前没有自定义权限。</div>
        <div th:unless="${permissions.isEmpty()}" class="table-container">
            <table class="table table-borderless table-hover">
                <thead>
                <tr>
                    <th>权限名称</th>
                    <th>过期时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="permission : ${permissions}">
                    <td>
                        <span class="badge bg-secondary permission-badge" th:text="${permission.permission}"></span>
                    </td>
                    <td>
                        <span th:text="${permission.expiresAt != null ? #temporals.format(permission.expiresAt, 'yyyy-MM-dd HH:mm') : '永不过期'}"></span>
                    </td>
                    <td>
                        <form th:action="@{'/admin/users/' + ${user.id} + '/permissions/' + ${permission.id} + '/delete'}" method="post" class="d-inline">
                            <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                            <button type="submit" class="btn btn-action btn-delete">
                                <i class="bi bi-trash-fill"></i> 删除
                            </button>
                        </form>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>

    <div class="content-card" th:if="${user}">
        <h5 class="mb-4"><i class="bi bi-plus-circle"></i> 添加新权限</h5>
        <form th:action="@{'/admin/users/' + ${user.id} + '/permissions'}" method="post">
            <div class="row">
                <div class="col-md-5 mb-3">
                    <label for="permission" class="form-label">权限名称</label>
                    <input type="text" class="form-control" id="permission" name="permission" required>
                </div>
                <div class="col-md-5 mb-3">
                    <label for="expiresAt" class="form-label">过期时间（可选）</label>
                    <input type="datetime-local" class="form-control" id="expiresAt" name="expiresAt">
                </div>
                <div class="col-md-2 d-flex align-items-end mb-3">
                    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                    <button type="submit" class="btn btn-primary w-100"><i class="bi bi-plus"></i> 添加权限</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>